package com.cyandpos.pad.pickhangup;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.cyandpos.pad.R;
import com.cyandpos.pad.utils.TimeUtils;

import java.util.List;


public class HangUpListAdapter extends BaseAdapter {

    private List<String> ticketList; // 数据
    private LayoutInflater inflator;
    private Context context;
    private int selectedPosition = -1;

    /**
     * @param context mainActivity
     */
    public HangUpListAdapter(Context context, List<String> ticketInfo) {
        this.context = context;
        this.ticketList = ticketInfo;
    }

    /*
    * 获得数据总数
    */
    @Override
    public int getCount() {
        return ticketList.size();
    }

    /*
     * 根据索引为position的数据
     */
    @Override
    public Object getItem(int position) {
        return ticketList.get(position);
    }

    /*
     * 根据索引值获得Item的Id
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflator.inflate(R.layout.item_pick_hang_up, null);
            viewHolder = new ViewHolder();
            viewHolder.id = (TextView) convertView.findViewById(R.id.tv_id);
            viewHolder.code = (TextView) convertView.findViewById(R.id.tv_code);
            viewHolder.time = (TextView) convertView.findViewById(R.id.tv_time);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (selectedPosition == position) {
            convertView.setBackgroundColor(0xFFFBD8BE);//R.color.pink
        } else {
            convertView.setBackgroundColor(Color.WHITE);
        }
        setValues(position, viewHolder);
        return convertView;
    }

    private void setValues(int position, ViewHolder viewHolder) {

        String[] split = ticketList.get(position).split("/");
        String result = split[split.length - 1];
        String[] split_show = result.split("_");

        viewHolder.id.setText(String.valueOf(position + 1));
        viewHolder.time.setText(TimeUtils.transFormatToFormant(split_show[2], TimeUtils.FORMAT_FOUR, TimeUtils.FORMAT_TWO_2));
        viewHolder.code.setText(split_show[1]);
    }


    public void setSelectPosition(int position) {
        selectedPosition = position;
    }


    private class ViewHolder {
        private TextView time;
        private TextView code;
        private TextView id;
    }
}
